package ihm.tabs;

import ihm.overlays.ElementListener;
import ihm.overlays.FriendOverlay;
import ihm.overlays.ListElementsOverlay;
import ihm.widgets.Element;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

public class FriendsTab extends Composite {

	private final ListElementsOverlay friendsPage;
	
	public FriendsTab(Composite father)
	{
		super(father, SWT.NONE);
		final StackLayout stackLayout = new StackLayout();
		setLayout(stackLayout);
		
		final FriendOverlay friendPage = new FriendOverlay(this);
		friendsPage = new ListElementsOverlay(this);
		
		// setup event handling
		friendsPage.addElementListener(new ElementListener(){
			@Override
			public void elementClicked(Element user) {
				// should lookup in the users objects i guess.
				friendPage.setPicture(user.getPicture());
				friendPage.setUserName(user.getUserName());
				
				stackLayout.topControl = friendPage;
				layout();
				friendPage.layout();
			}
		});
		
		friendPage.addReturnButtonSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				stackLayout.topControl = friendsPage;
				layout();
				friendsPage.layout();
			}
		});
		
		stackLayout.topControl = this.friendsPage;
	}
	
	/**
	 * Add a new friend in the friend tab
	 * @param username to display
	 * @param picture user's picture
	 */
	public void addFriend(String username, Image picture)
	{
		friendsPage.addElement(username, picture);
	}
}
